package com.xbongbong.paas.help;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.redis.RedisPrefixConstant;
import com.xbongbong.paas.redis.RedisTimeConstant;
import com.xbongbong.paas.toolbox.util.CollectionsUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.CompanyConfigEnum;
import com.xbongbong.sys.domain.entity.CompanyConfigEntity;
import com.xbongbong.sys.model.CompanyConfigModel;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * @author: cml
 * @since: 2023/3/16 11:21
 * @description:
 */
@Component
public class FileImportExportHelp {

    @Resource
    private PaasRedisHelper paasRedisHelper;
    @Resource
    private CompanyConfigModel companyConfigModel;

    /**
     * @Description: 根据图片string获得另外一种格式的string，示例如下
     * "["https://xbb/xbb.jpg","https://xbb/xbb2.jpg"]"      ->      "https://xbb/xbb.jpg\nhttps://xbb/xbb2.jpg"
     * @param imageString
     * @Author cml
     * @CreateTime 2023/3/8 17:00
     */
    public String getImageStringByImageInfo(String imageString){
        if (StringUtil.isEmpty(imageString)){
            return null;
        }
        List<String> imageList = JSONArray.parseArray(imageString, String.class);
        StringBuilder imageStringBuilder = new StringBuilder();
        if (CollectionsUtil.isNotEmpty(imageList)) {
            imageList.forEach(e -> imageStringBuilder.append(e.split("\\?")[0]).append(";\n"));
        }
        return imageStringBuilder.toString();
    }

    public String getFileStringByFileInfo(String fileString){
        if (StringUtil.isEmpty(fileString)){
            return null;
        }
        JSONArray jsonArray = JSONArray.parseArray(fileString);
        StringBuilder imageString = new StringBuilder();
        if (CollectionsUtil.isNotEmpty(jsonArray)) {
            for (int j = 0; j < jsonArray.size(); j++) {
                JSONObject fileJson = jsonArray.getJSONObject(j);
                imageString.append(fileJson.getString("filename")).append("|").append(fileJson.getString("attachIndex")).append(";\n");
            }
        }
        return imageString.toString();
    }

    /**
     * @Description: 判断corpid是否在导入导出的白名单中
     * @param corpid
     * @param whiteListType
     * @Author cml
     * @CreateTime 2023/3/14 11:04
     */
    public boolean isWhiteListByCorpidAndType(String corpid, String whiteListType) {
        Set value4Set = paasRedisHelper.getValue4Set(RedisPrefixConstant.MIGRATION_WHITE_LIST, whiteListType);
        if (Objects.nonNull(value4Set)) {
            return value4Set.contains(corpid);
        }
        Set<String> set = new HashSet<>();
        CompanyConfigEntity companyConfigEntity = companyConfigModel.getByConfigAlias(CompanyConfigEnum.MIGRATION_WHITE_LIST.getAlias(), StringConstant.DEFAULT_CORPID);
        if (Objects.nonNull(companyConfigEntity)) {
            JSONObject jsonObject = JSONObject.parseObject(companyConfigEntity.getConfigValue());
            JSONArray exportWhiteCorpidList = jsonObject.getJSONArray(whiteListType);
            if (CollectionsUtil.isNotEmpty(exportWhiteCorpidList)) {
                set = new HashSet<>(exportWhiteCorpidList.toJavaList(String.class));
            }
        }
        paasRedisHelper.setValue4Set(RedisPrefixConstant.MIGRATION_WHITE_LIST, whiteListType, set, RedisTimeConstant.HUGE_DURATION);
        return set.contains(corpid);
    }
}
